package com.lagou.servlet;

import com.lagou.db.DBCPUtils;
import com.lagou.zk.DbConfZK;
import com.lagou.zk.ZkStrSerializer;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.commons.dbcp.BasicDataSource;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

public class DbServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {

        //初始化数据库连接
        initDbcp();

        //监控节点变化
        ZkClient zkClient = new ZkClient("node01:2181");
        zkClient.setZkSerializer(new ZkStrSerializer());

        zkClient.subscribeDataChanges("/dbcp", new IZkDataListener() {

            //监听数据库连接池配置参数变化
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                //如果配置参数变化, 重新初始化DBCPUtils类的dataSource
                System.out.println("db pool parameters has deleted");
                initDbcp();
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                //监听数据库连接池配置参数被删除
            }
        });
    }

    public void initDbcp() {

        BasicDataSource dataSource = new BasicDataSource();
        HashMap<String, String>  dbConf = DbConfZK.getDbConf();
        dataSource.setDriverClassName(dbConf.get("driver_name"));
        dataSource.setUrl(dbConf.get("url"));
        dataSource.setUsername(dbConf.get("username"));
        dataSource.setPassword(dbConf.get("password"));

        DBCPUtils.dataSource = null;
        DBCPUtils.dataSource = dataSource;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("hello, world!");

        ArrayList<String> lists = null;
        try {
            Connection conn = DBCPUtils.getConnection();

            String sql = "show tables";
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            lists = new ArrayList<>();
            while (resultSet.next()) {
                lists.add(resultSet.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        PrintWriter writer = resp.getWriter();
        writer.write("this database has tables:\r\n");
        if (null != lists) {
            writer.write(lists.toString());
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
